package com.kgc.service.base.handler;


import com.kgc.common.result.Result;
import com.kgc.common.result.ResultCode;
import com.kgc.common.utils.ExceptionUtil;
import com.kgc.service.base.exception.HpitException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobaExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.error();
    }

    //特定异常
    @ExceptionHandler
    @ResponseBody
    public Result error(BadSqlGrammarException e){
        e.printStackTrace();
        return Result.setResult(ResultCode.BAD_SQL_GRAMMAR);
    }

    //自定义异常
    @ExceptionHandler
    @ResponseBody
    public Result error(HpitException e){
        log.error(ExceptionUtil.getMessage(e));
        return Result.error().setMessage(e.getMessage()).setCode(e.getCode());
    }
}
